<?php
namespace Home\Controller;
use Common\Controller\CommonController;

/**
 * 集采客服
 * @author 张亚伟 <zhangyw@zhijin101.com>
 */
class JcServiceController extends CommonController
{
    public function index()
    {
        $map = [];
        list($list, $page) = $this->lists('JcService', $map, 'id desc');
        $this->assign('page', $page);
        $this->assign('list', $list);
        $this->display();
    }

    public function add(){
        if(IS_POST){
            $data = I('post.');
            if(empty($data['name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入姓名'));
            }
            if(empty($data['phone'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入电话'));
            }
            if(empty($data['img'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传图片'));
            }
            $model = M('JcService');
            $id = $model->add($data);
            if($id){
                $this->ajaxReturn(array('s'=>true,'m'=>'添加成功', 'url'=>U('index')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'添加失败!'));
            }
        }else{
            $this->display();
        }
    }

    public function edit(){
        $id = I('id');
        if (IS_POST) {
            $data = I('post.');
            if(empty($data['name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入姓名'));
            }
            if(empty($data['phone'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入电话'));
            }
            if(empty($data['img'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传图片'));
            }

            $model = D('JcService');
            $res = $model->where(['id'=>$id])->save($data);
            if($res!==false){
                $this->ajaxReturn(array('s'=>true,'m'=>'修改成功', 'url'=>U('index')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
            }
        } else {
            $info = M('JcService')->where(['id' => $id])->find();
            $this->assign('info', $info);
            $this->display();
        }
    }

    public function del()
    {   
        $id = I('id');
        $res = M('JcService')->where(['id'=>$id])->delete();
        if($res){
            $this->ajaxReturn(array('s'=>true,'m'=>'删除成功', 'url'=>U('index')));
        }else{
            $this->ajaxReturn(array('s'=>false,'m'=>'删除失败!'));
        }
    }
}